package com.codegenerate.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.entity.SysDictionary;
import com.codegenerate.demo.entity.SysDictionaryDto;
import com.codegenerate.demo.service.impl.SysDictionaryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/sysDictionary")
public class SysDictionaryController {
    @Autowired
    SysDictionaryServiceImpl sysDictionaryService;


    @PostMapping("/list")
    R<List<SysDictionary>> list(@RequestBody SysDictionaryDto sysDictionaryDto){
        Page<SysDictionary> page = sysDictionaryService.page(
                sysDictionaryDto.buildPage(),sysDictionaryDto.dto2entity().getQueryAll()
        );
        return R.ok(page.getRecords())
                .setCount(page.getTotal());
    }

    @GetMapping("/getItem")
    R<SysDictionary> getItem(int id) {
        return R.ok(sysDictionaryService.getById(id));
    }

    @PostMapping("/add")
    R<Long> add(@RequestBody  SysDictionaryDto sysDictionaryDto){
        var entity=sysDictionaryDto.dto2entity();
        //设置 默认
        SysDictionary.setDefaultValue(entity);
        return sysDictionaryService.saveForValidate(entity);
    }


    @PostMapping("/update")
    R<Integer> update(@RequestBody  SysDictionaryDto sysDictionaryDto){
        sysDictionaryDto.validateUpdate();
        return R.ok(sysDictionaryService.getBaseMapper().update(null, sysDictionaryDto.dto2entity().getUpdateWrapper()));
    }

    @GetMapping("/delbyid")
    R<Boolean> del(@RequestParam("id") int id) {
        return R.ok(sysDictionaryService.removeById(id));
    }
}




